import 'package:example/app/data/model/article_bean.dart';
import 'package:example/app/modules/main_module/home_page/logic.dart';
import 'package:example/app/routes/app_pages.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

import 'article_item.dart';
import 'home_shimmer_box.dart';

class Body extends GetView<HomePageLogic> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomePageLogic>(
      init: controller,
      builder: (logic) {
        return Container(
          child: ViewStateWidget<HomePageLogic>(
            loadingWidget: HomeShimmerBox(),
            successWidget: SmartRefresher(
              controller: logic.refreshController,
              enablePullDown: true,
              enablePullUp: true,
              onRefresh: logic.onRefresh,
              onLoading: logic.onLoadMore,
              child: CustomScrollView(
                slivers: [
                  /// Banner
                  SliverToBoxAdapter(
                    child: Container(
                      width: Get.infinity,
                      height: 160.dp,
                      margin: EdgeInsets.only(top: 10.dp),
                      child: Swiper(
                        itemCount: logic.bannerList.length,
                        itemBuilder: (BuildContext context, int index) {
                          return GestureDetector(
                            onTap: () {
                              var banner = logic.bannerList[index];
                              Get.toNamed(Routes.ARTICLE_DETAIL,
                                  arguments: ArticleBean()
                                    ..id = banner.id
                                    ..title = banner.title
                                    ..link = banner.url
                                    ..collect = false);
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 10.dp),
                              child: ImageView(
                                url: logic.bannerList[index].imagePath,
                                borderRadius: BorderRadius.circular(6.dp),
                                fit: BoxFit.fill,
                              ),
                            ),
                          );
                        },
                        pagination: new SwiperPagination(),
                        autoplay: true,
                      ),
                    ),
                  ),

                  /// 置顶文章
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        return ArticleItem(
                          index: index,
                          item: logic.topArticles[index],
                          isTop: true,
                          onTap: (ArticleBean item) {
                            Get.toNamed(Routes.ARTICLE_DETAIL, arguments: item);
                          },
                        );
                      },
                      childCount: logic.topArticles.length,
                    ),
                  ),

                  /// 首页文章列表
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        return ArticleItem(
                          index: index,
                          item: logic.articles[index],
                          onTap: (ArticleBean item) {
                            Get.toNamed(Routes.ARTICLE_DETAIL, arguments: item);
                          },
                        );
                      },
                      childCount: logic.articles.length,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
